<template>
  <div class="dashboard-container">
    <el-card>
      <!-- 学科查询 + 新增学科 -->
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col
          :span="11"
        ><div class="grid-content bg-purple">
          <el-form :inline="true">
            <el-form-item label="学科名称">
              <el-input v-model="subjectName" size="small" />
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="subjectNames">清除</el-button>
              <el-button type="primary" size="small" @click="btnsearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </div></el-col>
        <el-col :span="1.5">
          <div class="grid-content bg-purple">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="showDialog = true"
            >新增学科</el-button>
          </div></el-col>
      </el-row>
      <!-- 学科查询 + 新增学科 -->
      <!-- 提示消息 -->
      <el-alert
        :title="`数据一共${counts}条`"
        :closable="false"
        type="info"
        show-icon
        class="subjects-alert"
      />
      <!-- 提示消息 -->
      <!-- 学科展示区域 -->
      <el-table :data="fromSubject" style="width: 100%">
        <el-table-column fixed type="index" label="序号" width="50" />
        <el-table-column prop="subjectName" label="学科名称" width="210" />
        <el-table-column prop="username" label="创建者" width="210" />
        <el-table-column prop="addDate" label="创建日期" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | datefmt('YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="205">
          <template slot-scope="scope">
            <span>{{ scope.row.isFrontDisplay === 1 ? '是' : '否' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录" width="205" />
        <el-table-column prop="tags" label="标签" width="205" />
        <el-table-column prop="totals" label="题目数量" width="205" />
        <el-table-column fixed="right" label="操作" width="230">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="$router.push({path:'directorys',query:{id:scope.row.id}})"
            >学科分类</el-button>
            <el-button type="text" size="small" @click="$router.push({path:'tags',query:{id:scope.row.id} })">学科标签</el-button>
            <el-button type="text" size="small" @click="modification(scope.row)">修改</el-button>
            <el-button type="text" size="small" @click="btnremove(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" :gutter="20">
        <el-pagination
          background
          :page-sizes="[5, 10, 20, 50]"
          layout="total, prev, pager, next, jumper , sizes"
          :current-page="list.page"
          :total="counts"
          :page-size="10"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-row>

      <!-- 学科展示区域-->
      <!-- 新增编辑弹框 -->
      <Theneweditor ref="Theneweditor" :show-dialog.sync="showDialog" />
      <!-- 新增编辑弹框 -->
    </el-card>
  </div>
</template>

<script>
// 引入api接口
import { list, remove } from '@/api/hmmm/subjects'
// 新增或者编辑组件
import Theneweditor from '../components/Theneweditor'
export default {
  // 注册组件
  components: {
    Theneweditor
  },
  data() {
    return {
      subjectName: null, // 查询学科输入框
      fromSubject: [
      ], // 后台返回数据
      counts: 0, // 一共多少条数据
      list: {
        page: 1,
        pagesize: 10
      },
      showDialog: false, // 控制新增或者编辑弹框
      pages: 1,
      items: []
    }
  },
  watch: {
    showDialog() {
      this.SubjectSimpleList()
    }
  },
  created() {
    this.SubjectSimpleList()
  },
  methods: {
    // 获取页面数据
    async SubjectSimpleList() {
      if (this.subjectName) {
        const { data } = await list({ subjectName: this.subjectName, page: this.list.page, pagesize: this.list.pagesize })
        this.fromSubject = data.items
        this.counts = data.counts
        this.pages = data.pages
      } else {
        const { data } = await list(this.list)
        // console.log(data)
        this.fromSubject = data.items
        this.counts = data.counts
        this.pages = data.pages
      }
    },
    // 删除学科
    btnremove(id) {
      this.$confirm('此操作永久删除此条数据', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async() => {
          await remove({ id: id })

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          const totalPage = Math.ceil((this.counts - 1) / this.list.pagesize) // 总页数
          const currentPage = this.list.page > totalPage ? totalPage : this.list.page
          this.list.page = currentPage < 1 ? 1 : currentPage
          this.SubjectSimpleList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      // console.log(id) 测试获取id
    },
    // 修改学科
    modification(row) {
      // console.log(row) 测试
      this.$refs.Theneweditor.fromSubject = row
      this.showDialog = true
    },
    // 页面跳转
    handleCurrentChange(val) {
      this.list.page = val
      this.SubjectSimpleList()
    },
    // 调整页码数量
    handleSizeChange(val) {
      // console.log(val)
      this.list.pagesize = val
      this.SubjectSimpleList()
    },
    // 查询列表
    async btnsearch() {
      this.list.page = 1
      // console.log(data[0])
      this.SubjectSimpleList()
    },
    subjectNames() {
      this.subjectName = ''
      this.SubjectSimpleList()
    }
  }
}
</script>

<style scoped>
.subjects-alert {
  margin-bottom: 10px;
}
</style>
